//
//  SQLCommandHandle.m
//  SQL面向对象数据库封装
//
//  Created by 崔博 on 2018/9/25.
//  Copyright © 2018年 崔博. All rights reserved.
//

#import "SQLCommandHandle.h"
#import <sqlite3.h>
#import "SQLDataBase.h"
@implementation SQLCommandHandle
// 第一步创建db


static sqlite3 *database;

// 增删改   查
- (void)sqlExcuteWriteCommand {
    if ( !self.sqlCommand || self.sqlCommand.length == 0) {
        NSLog(@"没有可执行的命令");
        return ;
    }
    // 操作   ⚠️sqlite3_stmt 失败一定要释放 即进行sqlite3_finalize操作
    // sqlite3_stmt 默认不能重复在利用
    // 1 先语句
    // 和表结构有关系
    
    
    sqlite3_stmt *stmt; // 可以理解为 代表一条指令
    // 2 检测语句
    int status = sqlite3_prepare_v2(self.dataBase.dataBaseSql, [self.sqlCommand UTF8String], -1, &stmt, 0);
    
    if (status != SQLITE_OK) {
        
        NSLog(@"sqlite3_prepare_v2 fail");
        sqlite3_finalize(stmt);
        return;
    }
    // 3 执行指令
    status = sqlite3_step(stmt);
    if (status != SQLITE_DONE) {
        NSLog(@"sqlite3_step fail");
    }
    // 4 结束
    sqlite3_finalize(stmt);
    
}


- (NSArray*)sqlExcuteReadCommand{
    if ( !self.sqlCommand || self.sqlCommand.length == 0) {
        NSLog(@"没有可执行的命令");
        return nil;
    }
    // 1 先语句
    sqlite3_stmt *stmt; // 可以理解为 代表一条指令
    // 2 检测语句
    int status = sqlite3_prepare_v2(self.dataBase.dataBaseSql, [self.sqlCommand UTF8String], -1, &stmt, 0);
    
    if (status != SQLITE_OK) {
        
        NSLog(@"sqlite3_prepare_v2 fail");
        sqlite3_finalize(stmt);
        return nil;
    }
    // 3 执行指令
    NSMutableArray *recordAry = [NSMutableArray new];
    while(sqlite3_step(stmt) == SQLITE_ROW){
        
        
        
        int columns = sqlite3_column_count(stmt);
        
        NSMutableDictionary *infoDict = [NSMutableDictionary new];
        for (int i = 0; i < columns; i++) {
            
            char *keyC = (char *)sqlite3_column_name(stmt, i);
            NSString *keyStr = [NSString stringWithUTF8String:keyC];
            
            char *valueC = (char *)sqlite3_column_text(stmt, i);
            NSString *valueStr = [NSString stringWithUTF8String:valueC];
            
            [infoDict setObject:valueStr forKey:keyStr];
            
        }
        [recordAry addObject:infoDict];
        
        
        //        char *idC   =  (char*)sqlite3_column_text(stmt, 0);
        //        char *nameC = (char*)sqlite3_column_text(stmt, 1);
        //
        //        NSString *idStr = [NSString stringWithUTF8String:idC];
        //        NSString *nameStr = [NSString stringWithUTF8String:nameC];
        //        NSMutableDictionary *infoDict = [NSMutableDictionary new];
        //        [infoDict setObject:idStr forKey:@"id"];
        //        [infoDict setObject:nameStr forKey:@"name"];
        //        [recordAry addObject:infoDict];
    }
    
    // 4 结束
    sqlite3_finalize(stmt);
    
    return recordAry;
}

// 第三步操作表


// 第四步 关闭

+ (void)closeSQL{
    
    sqlite3_close_v2(database);
    
}
- (SQLDataBase *)dataBase
{
    if (!_dataBase) {
        self.dataBase = [[SQLDataBase alloc] initWithFileName:nil];
    }
    return _dataBase;
}

@end
